<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PrettyProlog implementation details - the System Predicates</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="Emacs">
<style type="text/css">
@import url("../style.css");
</style>
</head>
<body>
<div id="container">
    <div id="header">
        <h1>PrettyProlog - the System Predicates</h1>
    </div>
    <div id="content">
	<p align="left">
	  Certain predicates can't be written in pure Prolog; others can be, but it is inefficient to do so. For such cases, PrettyProlog provides a way of adding predicates to the Engine in the form of Java classes inheriting from <code>prettyprolog.engine.syspreds.Syspred</code>. These are called &quot;system predicates&quot;, &quot;built-in predicates&quot; or &quot;syspreds&quot;. A system predicate must implement two methods:
	<ul>
	  <li>call(), which gets passed some arguments that define the environment in which the syspred is called, and determines the effect of the syspred (both via the return value and the method's side effects).</li>
	  <li>isBacktrackable(), which returns a boolean flag that is used by the Engine do determine what to do in case of backtracking. If this flag is true, the syspred is called again, else backtracking proceeds to the next stack frame, if any.</li>
	</ul>
	To register a system predicate with the Engine, use its registerSyspred(Symbol, int, Syspred) method. Currently, system predicate support is quite basic; in particular, checking on the number of arguments passed is demanded to the syspred itself, and no control is made to avoid a syspred to shadow a Clause in the theory.<br />
	Currently implemented system predicates include true, fail, !, assert, retract, =, ==, \=, \==, read, write, functor, arg, and others.
	</p>
	<p align="left">
	  <h4>The cut (!) special system predicate.</h4>
	  A very special syspred is the cut, or !. This is really more than a predicate because it has certain side effects on the solving process. Basically it cuts (hence the name) parts of the SLD tree, specifically it makes the engine ignore any other choice (ramification) from when the goal containing the cut is first pushed on the stack to when the cut is executed. It was implemented as follows.<br />
	  First, the Engine checks, every time it pushes a new frame on the stack, if its goal contains a cut. If it does, it increments a special flag, the &quot;cut mark&quot;. This is an integer, initially set to 0, that is stamped on each frame when it is pushed on the stack. When the cut is executed, it looks at this flag on the top frame (which is the one that contains the cut at the time it is executed) and removes every frame which has the same cut mark as the top one. It does so by calling the Stack's cut() method, instead of pop(), so that the Stack can generate a different event to differentiate between a normal pop operation and a cut. This implementation makes frames more memory consuming, but since this is a didactic interpreter efficiency is not a priority.
	</p>
	<p align="left">
	  <h4>Extending PrettyProlog: Adding new system predicates</h4>
	  Adding a new system predicate is quite simple. First, you should create a subclass of Syspred. This means implementing the isBacktrackable() method so that it returns true or false, depending on whether you want your predicate to be re-executed on backtracking or not, and implementing the call() method to do whatever the syspred does. The return value of call() is a class containing a flag which indicates if the predicate succeeded or not and a Substitution which is the one the Engine will use to obtain the next goal (i.e. if this Substitution includes the mapping X = 3 every further occurrence of X in the current goal will be replaced by 3).<br />
	  When you have your class, you can add it to the engine using the registerSyspred() method. Every time the engine encounters a Callable whose name and number of arguments equal those you passed to registerSyspred(), it'll call your Syspred's call() method. That's all. You can also subclass Engine and make your syspred be added automatically by the registerDefaultSyspreds() method.
	</p>
    </div>
    <div id="footer"><a href="../index.html">General info</a> | <a href="../javadoc/index.html">Javadocs</a> | <a href="index.html">Implementation details</a></div>
</div>
</body>
</html>